const Base = require('../base.js');

module.exports = class extends Base {
    indexAction() {
        this.meta_title = '后台目录';
        this.action_id = 'menu';
        return this.display();
    }

    async listAction() {
        let posts = this.post();
        let key = posts.search;
        if (key != null) {
            const sList = await this.model('menu').where({m_title: ['like', '%' + key + '%']}).countSelect();
            return this.json(sList);
        } else {
            const sList = await this.model('menu').countSelect();
            return this.json(sList);
        }
    }

    async addAction() {
        if (this.isPost) {
            const data = this.post();

            //return this.json({success:true});
            let res = true;
            if (res) {
                return this.json({errno: 0, title: '成功', message: '保存成功!页面即将自动跳转'});
            } else {
                return this.json({errno: 1000, title: '数据错误!', message: '系统接收错误'});
            }
        }
        else {
            return this.display();
        }
    }

    async editAction() {
        if (this.isPost) {
            const data = this.post();

            //return this.json({success:true});
            let res = false;
            if (res) {
                return this.json({errno: 0, title: '成功', message: '保存成功!页面即将自动跳转'});
            } else {
                return this.json({errno: 1000, title: '数据错误!', message: '系统接收错误'});
            }
        }
        else {
            const ids = parseInt(this.get('id'));
            const finddata = await this.model('menu').find(ids);
            //this.assign('menu', finddata);
            //this.meta_title = '编辑目录[' + finddata.m_title + ']';
            if (finddata) {
                return this.json(finddata);
            }
            else {
                return this.json();
            }
            // return this.display();
        }
    }

    async delAction() {
        const ids = this.get('id');
        // console.log(id);
        let res = false;
        if (res) {
            return this.json({errno: 0, title: '成功', message: '删除成功!'});
        } else {
            return this.json({errno: 1000, title: '错误!', message: '删除失败'});
        }
    }

    async chstaAction() {
        const map = {};
        // console.log(gets);
        if (this.get('key') == 1) {
            map.m_develop = this.get('status');
        } else {
            map.m_tf = this.get('status');
        }
        let sid = this.get('id');
        const res = await this.model('menu').where({id: sid}).update(map);
        if (res) {
            //think.cache('adminenu', null);// 清除菜单缓存
            return this.json(res);
        }
    }
};
